<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:15
 */

namespace App\GraphQL\Mutations;

use Closure;
use App\User;
use App\Model\Job;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Mutation;

class CreateJobMutation extends Mutation
{

    protected $attributes = [
        'name' => 'createJob'
    ];

    public function type(): Type
    {
        return \GraphQL::type('jobsType');//相当于表
    }

    public function args(): array
    {
        return [

            'userId' => [
                'name' => 'userId',
                'type' => Type::id()
            ],
            'name' => [
                'name' => 'name',
                'type' => Type::nonNull(Type::string())
            ],
            'description' => [
                'name' => 'description',
                'type' => Type::string()
            ]
        ];
    }



    public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
    {

    $s = <<<TEST
    
      //注意最外面不需要大括号, 小写的mutation
      mutation{
        createJob(userId: 1, name: "GO 后端工程师", description: "aaa"){
          id
          name
        }
      }
    
TEST;

    $job = new Job([
        'name' => $args['name'],
        'description' => $args['description'],
    ]);
    $user = User::find($args['userId']);

    if(!$user) {
        return null;
    }
    $user->job()->save($job);
    return $job;

    }
}
